package com.eedi.framework.dict.service;

import cn.hutool.core.util.StrUtil;
import com.google.common.annotations.VisibleForTesting;
import com.eedi.framework.dict.controller.sys.vo.type.SysDictTypePageReq;
import com.eedi.framework.dict.dal.dataobject.SysDictTypeDO;
import com.eedi.framework.dict.dal.mapper.SysDictTypeMapper;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.common.util.object.BeanUtils;
import com.eedi.framework.dict.controller.sys.vo.type.SysDictTypeSaveReq;
import com.eedi.framework.dict.controller.sys.vo.type.SysDictTypeUpReq;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

import static com.eedi.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.eedi.framework.dict.enums.SysDictErrorCodeConstants.*;

/**
 * 字典类型 Service 实现类
 *
 */
@Service
public class SysDictTypeServiceImpl implements SysDictTypeService {
//
//    @Resource
//    private SysDictDataService sysDictDataService;

    @Resource
    private SysDictTypeMapper sysDictTypeMapper;

    @Override
    public PageResult<SysDictTypeDO> getDictTypePage(SysDictTypePageReq pageReqVO) {
        return sysDictTypeMapper.selectPage(pageReqVO);
    }

    @Override
    public SysDictTypeDO getDictType(String type) {
        return sysDictTypeMapper.selectByType(type);
    }

    @Override
    public SysDictTypeDO getDictTypeByID(String id) {
        return sysDictTypeMapper.selectById(id);
    }

    @Override
    public String createDictType(SysDictTypeSaveReq createReqVO) {
        // 校验字典类型的名字的唯一性
        validateDictTypeNameUnique(null, createReqVO.getSysDictName());
        // 校验字典类型的类型的唯一性
        validateDictTypeUnique(null, createReqVO.getSysDictDataType());

        // 插入字典类型
        SysDictTypeDO dictType = BeanUtils.toBean(createReqVO, SysDictTypeDO.class);
        sysDictTypeMapper.insert(dictType);
        return dictType.getSysDictTypeId();
    }

    @Override
    public void updateDictType(SysDictTypeUpReq updateReqVO) {
        // 校验自己存在
        validateDictTypeExists(updateReqVO.getSysDictTypeId());
        // 校验字典类型的名字的唯一性
        validateDictTypeNameUnique(updateReqVO.getSysDictTypeId(), updateReqVO.getSysDictName());
        // 校验字典类型的类型的唯一性
        validateDictTypeUnique(updateReqVO.getSysDictTypeId(), updateReqVO.getSysDictDataType());

        // 更新字典类型
        SysDictTypeDO updateObj = BeanUtils.toBean(updateReqVO, SysDictTypeDO.class);
        sysDictTypeMapper.updateById(updateObj);
    }

    @Override
    public void deleteDictType(String id) {
        // 校验是否存在
        SysDictTypeDO dictType = validateDictTypeExists(id);
        // 校验是否有字典数据 逻辑删除，字典数据存在亦可
//        if (sysDictDataService.getDictDataCountByDictType(dictType.getSysDictDataType()) > 0) {
//            throw exception(SYS_DICT_TYPE_HAS_CHILDREN);
//        }
        // 删除字典类型
        sysDictTypeMapper.updateToDelete(id, LocalDateTime.now());
    }

    @Override
    public List<SysDictTypeDO> getDictTypeList() {
        return sysDictTypeMapper.selectList();
    }

    @VisibleForTesting
    void validateDictTypeNameUnique(String id, String name) {
        SysDictTypeDO dictType = sysDictTypeMapper.selectByName(name);
        if (dictType == null) {
            return;
        }
        // 如果 id 为空，说明不用比较是否为相同 id 的字典类型
        if (id == null) {
            throw exception(SYS_DICT_TYPE_NAME_DUPLICATE);
        }
        if (!dictType.getSysDictTypeId().equals(id)) {
            throw exception(SYS_DICT_TYPE_NAME_DUPLICATE);
        }
    }

    @VisibleForTesting
    void validateDictTypeUnique(String id, String type) {
        if (StrUtil.isEmpty(type)) {
            return;
        }
        SysDictTypeDO dictType = sysDictTypeMapper.selectByType(type);
        if (dictType == null) {
            return;
        }
        // 如果 id 为空，说明不用比较是否为相同 id 的字典类型
        if (id == null) {
            throw exception(SYS_DICT_TYPE_TYPE_DUPLICATE);
        }
        if (!dictType.getSysDictTypeId().equals(id)) {
            throw exception(SYS_DICT_TYPE_TYPE_DUPLICATE);
        }
    }

    @VisibleForTesting
    SysDictTypeDO validateDictTypeExists(String id) {
        if (id == null) {
            return null;
        }
        SysDictTypeDO dictType = sysDictTypeMapper.selectById(id);
        if (dictType == null) {
            throw exception(SYS_DICT_TYPE_NOT_EXISTS);
        }
        return dictType;
    }

}
